#import "SEGRootViewModel.h"

@interface SEGRootViewModel()
@end

@implementation SEGRootViewModel
#pragma mark - Init
-(instancetype)init{
    if (self = [super init]) {
        NSLog(@"%s",__func__);
        _contentDataSource = [NSArray array];
        [self racInit];
    }
    return self;
}

-(void)dealloc {
    NSLog(@"%s",__func__);
    [self.requestCommand rac_deallocDisposable];
    [self.messageSubject rac_deallocDisposable];

// 微信一键登陆-RAC
[self.wxCodeToLoginCommand rac_deallocDisposable];
// 快速注册并自动登录-RAC
[self.fastRegisterCommand rac_deallocDisposable];
// 手机号一键登录-RAC
[self.loginByTelCommand rac_deallocDisposable];
// 根据极光token获取用户信息-RAC
[self.getTelByJgTokenCommand rac_deallocDisposable];
// 检验短信并自动登录-RAC
[self.validSmsCommand rac_deallocDisposable];
// 绑定微信-RAC
[self.wxBindCommand rac_deallocDisposable];
// 绑定苹果-RAC
[self.iosBindCommand rac_deallocDisposable];
// 苹果一键登陆-RAC
[self.iosCodeToLoginCommand rac_deallocDisposable];
// 获取默认小区-RAC
[self.getDefaultCommunityCommand rac_deallocDisposable];
// 选择小区-RAC
[self.queryCommunityListV2Command rac_deallocDisposable];
}

- (void)racInit {
    [self seg_requestCommand];

// 微信一键登陆-RAC
[self seg_wxCodeToLoginCommand];
// 快速注册并自动登录-RAC
[self seg_fastRegisterCommand];
// 手机号一键登录-RAC
[self seg_loginByTelCommand];
// 根据极光token获取用户信息-RAC
[self seg_getTelByJgTokenCommand];
// 检验短信并自动登录-RAC
[self seg_validSmsCommand];
// 绑定微信-RAC
[self seg_wxBindCommand];
// 绑定苹果-RAC
[self seg_iosBindCommand];
// 苹果一键登陆-RAC
[self seg_iosCodeToLoginCommand];
// 获取默认小区-RAC
[self seg_getDefaultCommunityCommand];
// 选择小区-RAC
[self seg_queryCommunityListV2Command];
}

#pragma mark - Publish Methods

#pragma mark - Private Methods
/// show message
-(void)seg_showMessage:(NSString*)msg code:(NSInteger)code{
    [self.messageSubject sendNext:@{@"code":@(code),@"msg":msg}];
}

-(void)seg_requestCommand{
    //@weakify(self);
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);
            
            // Code here...
            // [subscriber sendNext:nil];
            // [subscriber sendCompleted];

            // NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:@"error!"}];
            // [subscriber sendError:error];            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

/// 微信一键登陆-RAC
-(void)seg_wxCodeToLoginCommand {
    _wxCodeToLoginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);
            
            // Code here...
            [self.rootManager requestWxCodeToLoginWithJsCode:@"" finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
                
                if (!isSuccessed) {
                    NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

/// 快速注册并自动登录-RAC
-(void)seg_fastRegisterCommand {
    _fastRegisterCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);
            
            // Code here...
            [self.rootManager requestFastRegisterWithTel:@"" communityId:@"" openId:@"" iosUserId:@"" finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
                
                if (!isSuccessed) {
                    NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

/// 手机号一键登录-RAC
-(void)seg_loginByTelCommand {
    _loginByTelCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);
            
            // Code here...
            [self.rootManager requestLoginByTelWithTel:@"" finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
                
                if (!isSuccessed) {
                    NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

/// 根据极光token获取用户信息-RAC
-(void)seg_getTelByJgTokenCommand {
    _getTelByJgTokenCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);
            
            // Code here...
            [self.rootManager requestGetTelByJgTokenWithJgToken:@"" finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
                
                if (!isSuccessed) {
                    NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

/// 检验短信并自动登录-RAC
-(void)seg_validSmsCommand {
    _validSmsCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);
            
            // Code here...
            [self.rootManager requestValidSmsWithSmsCode:@"" businessCode:@"" tel:@"" finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
                
                if (!isSuccessed) {
                    NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

/// 绑定微信-RAC
-(void)seg_wxBindCommand {
    _wxBindCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);
            
            // Code here...
            [self.rootManager requestWxBindWithOpenId:@"" tel:@"" finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
                
                if (!isSuccessed) {
                    NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

/// 绑定苹果-RAC
-(void)seg_iosBindCommand {
    _iosBindCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);
            
            // Code here...
            [self.rootManager requestIosBindWithIosUserId:@"" tel:@"" finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
                
                if (!isSuccessed) {
                    NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

/// 苹果一键登陆-RAC
-(void)seg_iosCodeToLoginCommand {
    _iosCodeToLoginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);
            
            // Code here...
            [self.rootManager requestIosCodeToLoginWithIosUserId:@"" finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
                
                if (!isSuccessed) {
                    NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

/// 获取默认小区-RAC
-(void)seg_getDefaultCommunityCommand {
    _getDefaultCommunityCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);
            
            // Code here...
            [self.rootManager requestGetDefaultCommunityWithFinished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
                
                if (!isSuccessed) {
                    NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

/// 选择小区-RAC
-(void)seg_queryCommunityListV2Command {
    _queryCommunityListV2Command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);
            
            // Code here...
            [self.rootManager requestQueryCommunityListV2WithCommunityName:@"" code:@"" finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
                
                if (!isSuccessed) {
                    NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

#pragma mark - Getter
-(RACSubject *)messageSubject{
    if (!_messageSubject) {
        _messageSubject = [RACSubject subject];
    }
    return _messageSubject;
}

-(SEGRootManager *)rootManager{
    if (!_rootManager) {
        _rootManager = [[SEGRootManager alloc]init];
    }
    return _rootManager;
}
#pragma mark - Stter
@end
